﻿namespace ufida.u9.ait.openapi
{
	using System;
    using System.Collections;
    using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Warehouse;
    using UFIDA.U9.ISV.SM.Proxy;
    using UFIDA.U9.SM.Ship;
    using UFIDA.U9.SM.SO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Transactions;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// ShipSaveFromSO partial 
	/// </summary>	
	public partial class ShipSaveFromSO 
	{	
		internal BaseStrategy Select()
		{
			return new ShipSaveFromSOImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ShipSaveFromSOImpementStrategy : BaseStrategy
	{
		public ShipSaveFromSOImpementStrategy() { }

		public override object Do(object obj)
		{						
			ShipSaveFromSO bpObj = (ShipSaveFromSO)obj;

            //启用UBF事务
            //using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
            //{
                try
                {
                    if (bpObj.Data == null || bpObj.Data.Count == 0)
                        throw new Exception("传入参数为空");
                    List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

                    foreach (ShipFromSODTOData dto in bpObj.Data)
                    {
                        preSave(dto);
                        //Ship item = Ship.Finder.Find($"DescFlexField.PrivateDescSeg1='{dto.DocNo}'");
                        Ship entity = Ship.Finder.Find($"DocNo='{dto.DocNo}'");
                        if (entity == null)
                        {
                            List<object> inputlist = DTOUtils.DTOListPropCopy(dto.SrcDocInfoDTOList.ConvertAll(item => item as object)
                                , typeof(UFIDA.U9.ISV.SM.SrcDocInfoDTOData));
                            //新增逻辑
                            SMPullSrcDocSVProxy proxy = new SMPullSrcDocSVProxy();
                            proxy.ShipDocTypeID = dto.DocTypeID;
                            proxy.ShipDate = DateTime.Parse(dto.ShipDate);
                            proxy.SrcDocInfoDTOList = inputlist.ConvertAll(o => o as UFIDA.U9.ISV.SM.SrcDocInfoDTOData);
                            List<long> ret = proxy.Do();
                            if (ret.Count == 0)
                                throw new Exception("生成出货单失败!");

                            //修改单号并返回
                            CommonArchiveDataDTOData retdto = new CommonArchiveDataDTOData
                            {
                                ID = ret[0],
                                Code = EntityUtils.UpdateDocNo<Ship>(ret[0], dto.DocNo)
                            };
                            retlist.Add(retdto);

                            //单据审核
                            ShipApproveSV approve = new ShipApproveSV();
                            approve.Data = new List<CopyOfCommonArchiveDataDTOData>
                            {
                                new CopyOfCommonArchiveDataDTOData
                                {
                                    ID=retdto.ID,
                                    Code =retdto.Code,
                                }
                            };
                            ResultDTOData resultDTOData = approve.Do();
                            ResultUtils.CheckResult(resultDTOData);
                        }
                        else
                        {
                            //修改逻辑
                            throw new AITException(599, $"单号:{dto.DocNo}已经存在,不能生成!");
                        }
                    }

                    //scope.Commit();
                    return ResultUtils.success(bpObj, bpObj.Data, retlist);
                }
                catch (Exception ex)
                {
                    //scope.Rollback();
                    return ResultUtils.error(bpObj, bpObj.Data, ex);
                }
            //}
            
        }

        private void preSave(ShipFromSODTOData dto)
        {
            //校验
            if (dto.DocNo == null || dto.DocNo.Length == 0)
                throw new Exception("传入单号为空");
            foreach (CopyOfSrcDocInfoDTOData input in dto.SrcDocInfoDTOList)
            {
                if (input.ItemMaster == null)
                    throw new Exception("传入物料[ItemMaster]为空");
            }

            //赋值
            ShipDocType docType = ShipDocType.Finder.Find($"Code='{dto.DocType}'");
            if (docType == null)
            {
                throw new Exception($"查找单据类型出错,编码:[{dto.DocType}]");
            }
            dto.DocTypeID = docType.ID;

            foreach (CopyOfSrcDocInfoDTOData input in dto.SrcDocInfoDTOList)
            {
                SOShipline shipline = SOShipline.Finder.Find($"DocSubLineNo={input.SubLineNo} and SOLine.DocLineNo={input.DocLineNo} and SOLine.SO.DocNo='{input.DocNo}'");
                if (shipline == null)
                {
                    throw new Exception($"来源订单查找失败!来源单号:[{input.DocNo} {input.DocLineNo} {input.SubLineNo}]");
                }
                input.SrcDocLineID = shipline.ID;
                if (shipline.ItemInfo.ItemCode != input.ItemMaster.Code)
                    throw new Exception($"来源料号[{shipline.ItemInfo.ItemCode}]与传入料号[{input.ItemMaster.Code}]不同!");

                Warehouse wh = Warehouse.Finder.Find($"Code='{input.WhCode}'");
                if (wh == null)
                {
                    throw new Exception($"仓库编码[{input.WhCode}]查找不到仓库!");
                }
                input.WH = wh.ID;
            }
        }
    }

	#endregion
	
	
}